package cn.alex.springaidemo.openAi;

import cn.alex.springaidemo.CommonConstant;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.audio.transcription.AudioTranscriptionPrompt;
import org.springframework.ai.audio.transcription.AudioTranscriptionResponse;
import org.springframework.ai.openai.OpenAiAudioSpeechModel;
import org.springframework.ai.openai.OpenAiAudioSpeechOptions;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.ai.openai.audio.speech.SpeechPrompt;
import org.springframework.ai.openai.audio.speech.SpeechResponse;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 音频模型
 *
 * @author Alex
 * @date 2024/9/20 01:18
 */
@RestController
@RequestMapping("/audio/model/openAi")
@RequiredArgsConstructor
public class AudioModelController {
    private static final String AUDIO_PATH = CommonConstant.FILE_STORE_PATH + "audio";
    private final OpenAiAudioTranscriptionModel audioTranscriptionModel;
    private final OpenAiAudioSpeechModel audioSpeechModel;

    /**
     * 语音转文字
     *
     * @return String
     */
    @GetMapping("transcription")
    public String transcription(@RequestParam("fileName") String fileName) {
        OpenAiAudioApi.TranscriptResponseFormat responseFormat = OpenAiAudioApi.TranscriptResponseFormat.VTT;
        OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
                .withLanguage("en")
                .withPrompt("Ask not this, but ask that")
                .withTemperature(0f)
                .withResponseFormat(responseFormat)
                .build();

        AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(new FileSystemResource(new File(AUDIO_PATH + fileName)), transcriptionOptions);
        AudioTranscriptionResponse response = audioTranscriptionModel.call(transcriptionRequest);
        return response.getResult().getOutput();
    }

    /**
     * 文字转语音
     */
    @GetMapping("speech")
    public String speech(@RequestParam(value = "msg", defaultValue = "您好，我是AI小助手，测试文字转语音") String msg) throws IOException {
        OpenAiAudioSpeechOptions speechOptions = OpenAiAudioSpeechOptions.builder()
                .withModel(OpenAiAudioApi.TtsModel.TTS_1.getValue())
                .withVoice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY)
                .withResponseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
                .withSpeed(1.0f)
                .build();

        SpeechResponse response = audioSpeechModel.call(new SpeechPrompt(msg, speechOptions));
        byte[] output = response.getResult().getOutput();

        String audioFilePath = AUDIO_PATH + System.currentTimeMillis() + ".mp3";
        File file = new File(audioFilePath);
        file.createNewFile();

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(output);
        fileOutputStream.close();

        return "生成的语音文件已保存：" + audioFilePath;
    }


}
